﻿using AnesSystem.Controls;
using AnesSystem.Model;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Udp;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace AnesSystem.ViewModels
{
    public class ConfigAccountCtrlViewModel : BaseViewModel<ConfigAccountCtrlViewModel>
    {
        #region Variables

        private string _value = "";
        private DispatcherTimer timer;
        private string _isAddorUpdate = "";

        private ObservableCollection<AccountModel> _disAccountList = new ObservableCollection<AccountModel>();

        #endregion

        #region Constructor
        public ConfigAccountCtrlViewModel()
        {
            InitData();
            InitalTimer();
        }


        #endregion

        #region Properties

        public ObservableCollection<AccountModel> DisAccountList
        {
            get
            {
                return _disAccountList;
            }
            set
            {
                _disAccountList = value;
                RaisePropertyChanged("DisAccountList");
            }
        }
        public string Value
        {
            get
            {
                return _value;
            }

            set
            {
                _value = value;
                SeachData();
                RaisePropertyChanged("DisAccountList");
            }
        }

        public string IsAddorUpdate
        {
            get
            {
                return _isAddorUpdate;
            }

            set
            {
                _isAddorUpdate = value;
            }
        }
        #endregion

        #region Event

        #endregion

        #region Private Methods

        private void SeachData()
        {
            DisAccountList.Clear();
            List<AccountModel> list = AccountListCtrlViewModel.Instance.AccountList.ToList().Where(O =>
              {
                  string st = "";
                  if (string.IsNullOrEmpty(O.OfficeName)) return false;
                  if(!string.IsNullOrEmpty(O.LoginName)&& !string.IsNullOrEmpty(O.TrueName))
                    st= O.LoginName + O.TrueName;
                  return st.Contains(_value) && (O.OfficeID.Equals("12125"));
              }).ToList();
            DisAccountList = list.ToObservableCollection();
        }

        private void InitalTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 20);
            timer.Tick += Timer_Tick;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            AccountListCtrlViewModel.Instance.Refresh();
            Refresh();
            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Collapsed;
            timer.Stop();
        }
        private void InitData()
        {
            SeachData();
            //FilterAccount();
        }

        #endregion

        #region Public/Protected Methods
        public void DelAccountModel(AccountModel accountModel)
        {
            if (accountModel != null)
            {
                DisAccountList.Remove(accountModel);
                AccountService.Instance.DeleteById(accountModel.Id);
            }
        }

        public void AddRow()
        {
            IsAddorUpdate = "ADD";
            AccountModel model = new AccountModel();
            AccountUpDateCtrl retypesettingCtrl = new AccountUpDateCtrl(model, true);
            DrugAndEventWindow.Instance.Title = "新建账户";
            DrugAndEventWindow.Instance.CusContent = retypesettingCtrl;
            DrugAndEventWindow.Instance.ShowDialog();



            //DisAccountList.Insert(0, model);
            //model.PropertyChanged += Model_PropertyChanged;
        }
        public void Update(AccountModel model)
        {
            IsAddorUpdate = "UPDATE";
            AccountUpDateCtrl retypesettingCtrl = new AccountUpDateCtrl(model, false);
            DrugAndEventWindow.Instance.Title = "修改账户";
            DrugAndEventWindow.Instance.CusContent = retypesettingCtrl;
            DrugAndEventWindow.Instance.ShowDialog();



            //DisAccountList.Insert(0, model);
            //model.PropertyChanged += Model_PropertyChanged;
        }

        public void getDeptAccount()
        {
            SeachData();
        }

        public void getAllAccount()
        {
            DisAccountList = AccountListCtrlViewModel.Instance.AccountList.ToObservableCollection();

        }


        public void getAccountFromHis()
        {
            Task.Factory.StartNew(delegate
            {
                UdpMangager.Instance.SendUdpServerMsg(ServiceSign.ACCCOUNT_ADD, new UdpModel());
                timer.Start();//执行查询方法的线程  
            });
            MainPageCommonViewModel.Instance.WaitCtrlVisibility = Visibility.Visible;
        }

        public override void Refresh()
        {
            InitData();
        }
        #endregion

        #region EventHandlers



        #endregion

        #region Command


        #endregion
    }
}
